/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * File Name          : freertos.c
 * Description        : Code for freertos applications
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under Ultimate Liberty license
 * SLA0044, the "License"; You may not use this file except in compliance with
 * the License. You may obtain a copy of the License at:
 *                             www.st.com/SLA0044
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "user_mb_app.h"
#include "tim.h"
#include "usart.h"
#include "dlom2_sensor.h"
#include "app_store.h"
#include "app_task.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
/* USER CODE END Variables */

/* Definitions for slaveTask */
osThreadId_t slaveTaskHandle;
const osThreadAttr_t slaveTask_attributes = { .name = "slaveTask", .stack_size =
		128 * 4, .priority = (osPriority_t) osPriorityNormal, };
/* Definitions for masterTask */
osThreadId_t masterTaskHandle;
const osThreadAttr_t masterTask_attributes = { .name = "masterTask",
		.stack_size = 128 * 4, .priority = (osPriority_t) osPriorityLow, };
/* Private function prototypes -----------------------------------------------*/
/* Definitions for updateTask */
osThreadId_t updateTaskHandle;
const osThreadAttr_t updateTask_attributes = { .name = "updateTask",
		.stack_size = 128 * 4, .priority = (osPriority_t) osPriorityLow, };
/* Definitions for pwmCtrlTask */
osThreadId_t pwmCtrlTaskHandle;
const osThreadAttr_t pwmCtrlTask_attributes = { .name = "pwmCtrlTask",
		.stack_size = 128 * 4, .priority = (osPriority_t) osPriorityNormal, };

/* USER CODE BEGIN FunctionPrototypes */
/* USER CODE END FunctionPrototypes */

void StartPwmCtrlTask(void *argument);
void StartUpdateTask(void *argument);
void StartMasterTask(void *argument);
void StartSlaveTask(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
 * @brief  FreeRTOS initialization
 * @param  None
 * @retval None
 */
void MX_FREERTOS_Init(void) {
	/* USER CODE BEGIN Init */
	HAL_TIM_Base_Start(&htim4);
	HAL_TIM_Base_Start(&htim2);

	printf("slave init \r\n");

	eMBInit(MB_RTU, 0x01, 2, 115200, MB_PAR_NONE);
	eMBEnable();
	//密度传感器主�?
	printf("app store init \r\n");
	app_store_init();

	printf("app task dlom2 poll init \r\n");
	dlom2_init(247);

	printf("app task pid ctrl init \r\n");
	app_task_pid_task_init();

	printf("app task update  init \r\n");
	app_task_check_update_init();
	/* USER CODE END Init */
	printf("app task init  start \r\n");
	/* USER CODE BEGIN RTOS_MUTEX */
	/* add mutexes, ... */
	/* USER CODE END RTOS_MUTEX */

	/* USER CODE BEGIN RTOS_SEMAPHORES */
	/* add semaphores, ... */
	/* USER CODE END RTOS_SEMAPHORES */

	/* USER CODE BEGIN RTOS_TIMERS */
	/* start timers, add new ones, ... */
	/* USER CODE END RTOS_TIMERS */

	/* USER CODE BEGIN RTOS_QUEUES */
	/* add queues, ... */
	/* USER CODE END RTOS_QUEUES */

	/* Create the thread(s) */

	/* creation of slaveTask */
	slaveTaskHandle = osThreadNew(StartSlaveTask, NULL, &slaveTask_attributes);

	/* creation of masterTask */
	masterTaskHandle = osThreadNew(StartMasterTask, NULL,
			&masterTask_attributes);

	/* creation of updateTask */
	updateTaskHandle = osThreadNew(StartUpdateTask, NULL,
			&updateTask_attributes);

	/* creation of pwmCtrlTask */
	pwmCtrlTaskHandle = osThreadNew(StartPwmCtrlTask, NULL,
			&pwmCtrlTask_attributes);

	/* USER CODE BEGIN RTOS_THREADS */
	/* add threads, ... */
	printf("app task init  done \r\n");
	/* USER CODE END RTOS_THREADS */

	/* USER CODE BEGIN RTOS_EVENTS */
	/* add events, ... */
	/* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartPwmCtrlTask */
/**
 * @brief  Function implementing the pwmCtrlTask thread.
 * @param  argument: Not used
 * @retval None
 */
/* USER CODE END Header_StartPwmCtrlTask */
void StartPwmCtrlTask(void *argument) {
	/* USER CODE BEGIN StartPwmCtrlTask */
	osDelay(10000);
	printf("pwm pid ctrl task start \r\n");

	/* Infinite loop */
	for (;;) {
		app_task_excute_output();
		osDelay(pid_cache.delay_ms);
	}
	/* USER CODE END StartPwmCtrlTask */
}

/* USER CODE BEGIN Header_StartUpdateTask */
/**
 * @brief Function implementing the updateTask thread.
 * @param argument: Not used
 * @retval None
 */
/* USER CODE END Header_StartUpdateTask */
void StartUpdateTask(void *argument) {
	/* USER CODE BEGIN StartUpdateTask */
	osDelay(5000);
	printf("update task start \r\n");

	/* Infinite loop */
	for (;;) {
		app_task_update_poll();
		osDelay(1000);
	}
	/* USER CODE END StartUpdateTask */
}

/* USER CODE BEGIN Header_StartMasterTask */
/**
 * @brief Function implementing the masterTask thread.
 * @param argument: Not used
 * @retval None
 */
/* USER CODE END Header_StartMasterTask */
void StartMasterTask(void *argument) {
	/* USER CODE BEGIN StartMasterTask */
	/* Infinite loop */
	osDelay(1000);
	printf("master task start\r\n");

	for (;;) {
		dlom2_read_value_poll();
		//modbus_test();
		osDelay(500);
	}
	/* USER CODE END StartMasterTask */
}

/* USER CODE BEGIN Header_StartSlaveTask */
/**
 * @brief Function implementing the slaveTask thread.
 * @param argument: Not used
 * @retval None
 */
/* USER CODE END Header_StartSlaveTask */
void StartSlaveTask(void *argument) {
	/* USER CODE BEGIN StartSlaveTask */

	printf("slave task start\r\n");
	/* Infinite loop */

	for (;;) {
		eMBPoll();
		osDelay(10);
	}
	/* USER CODE END StartSlaveTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

